_realname=ghdl
pkgbase="mingw-w64-${_realname}"
pkgname='__placeholder__'
pkgver=1.0.0.r964.g70f3c162b
pkgrel=2
pkgdesc='GHDL: the open-source analyzer, compiler, simulator and (experimental) synthesizer for VHDL'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('GPL2+')
url='https://github.com/ghdl'
groups=("${MINGW_PACKAGE_PREFIX}-eda")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")

_commit="70f3c162"
source=("${_realname}::git+https://github.com/ghdl/ghdl.git#commit=${_commit}")
sha512sums=('SKIP')

pkgver() {
  cd "ghdl"
  git describe --long "${_commit}" | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  if [ "$CARCH" = "i686" ]; then
    echo 'Configuring ghdl-mcode...'
    MSYS2_ARG_CONV_EXCL="--prefix" ../${_realname}/configure \
        --prefix=${MINGW_PREFIX} \
        --enable-checks \
        --enable-libghdl \
        --enable-synth \
        LDFLAGS="-static -Wl,--stack=8388608"
  fi

  if [ "$CARCH" = "x86_64" ]; then
    echo 'Configuring ghdl-llvm...'
    export CC=clang
    export CXX=clang++
    MSYS2_ARG_CONV_EXCL="--prefix" ../${_realname}/configure \
        --prefix=${MINGW_PREFIX} \
        --enable-checks \
        --enable-libghdl \
        --enable-synth \
        --with-llvm-config="llvm-config --link-static" \
        LDFLAGS="-static"
  fi

  make GNATMAKE="gnatmake -j$(nproc)"
}

check() {
  cd "${srcdir}/build-${CARCH}"
  make install.vpi.local
  make CC=gcc test
}

_package() {
  cd "${srcdir}"/build-${CARCH}
  _lib="${pkgdir}${MINGW_PREFIX}"/lib
  mkdir -p "${_lib}"
  make DESTDIR="${pkgdir}" install

  sed -i -e 's@.*\(/mingw.*\)@\1@' "${_lib}"/libghdl.link

  if [ "${CARCH}" = "x86_64" ]; then
    sed -i -e 's@^-L.*\(/mingw.*\)@-L\1@' "${_lib}"/ghdl/grt.lst
  fi

  # License
  install -Dm644 ${srcdir}/${_realname}/doc/licenses.rst ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/licenses.rst
}

if [ "${CARCH}" = "x86_64" ]; then
  pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-llvm")
  makedepends=(
    "${MINGW_PACKAGE_PREFIX}-clang"
    "${MINGW_PACKAGE_PREFIX}-autotools"
    "${MINGW_PACKAGE_PREFIX}-gcc-ada"
    "git"
  )
  package() {
    pkgdesc="$pkgdesc (LLVM backend) (mingw-w64)"
    depends=(
      "${MINGW_PACKAGE_PREFIX}-gcc-ada"
      "${MINGW_PACKAGE_PREFIX}-clang"
      "${MINGW_PACKAGE_PREFIX}-llvm"
      "${MINGW_PACKAGE_PREFIX}-zlib"
    )
    options=(!emptydirs)
    _package
  }
fi
if [ "${CARCH}" = "i686" ]; then
  pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-mcode")
  makedepends=(
    "${MINGW_PACKAGE_PREFIX}-cc"
    "${MINGW_PACKAGE_PREFIX}-autotools"
    "${MINGW_PACKAGE_PREFIX}-gcc-ada"
    "git"
  )
  package() {
    pkgdesc="$pkgdesc (mcode backend) (mingw-w64)"
    depends=(
      "${MINGW_PACKAGE_PREFIX}-gcc-ada"
      "${MINGW_PACKAGE_PREFIX}-zlib"
    )
    _package
  }
fi
